class Solution:
    def productExceptSelf(self, nums: list[int]) -> list[int]:
        k = len(nums)
        a = [0 for _ in range(k)]
        b = [0 for _ in range(k)]
        a[0] = b[-1] = 1
        for i in range(1, k):
            a[i] = a[i - 1] * nums[i - 1]
        for i in range(k - 2, -1, -1):
            b[i] = b[i + 1] * nums[i + 1]
        res = [0 for _ in range(k)]
        for i in range(k):
            res[i] = a[i] * b[i]
        return res
